/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package braipetro.data_access;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author manasses
 */
public class DataConnection {
    private String host;
    private String port;
    private String database;
    private String user;
    private String password;
    private Connection cnn;
    private Statement stm;
    
    
    public DataConnection() {
         
    }
    
//------------------------------------------------------------------------------
// GETs and SETERs
//------------------------------------------------------------------------------
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    
    
//------------------------------------------------------------------------------
// Private Methods
//------------------------------------------------------------------------------
    
    private void loadConnectionData(String file) {
        FileReader fr = null;
        Boolean ret = true;
        String dir = System.getProperty("user.dir");
        
        try {
            File f = new File(file);
            fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
            
            while (br.ready()) {
                String str = br.readLine();
                
                String[] tokens = str.split(":");
                
                if ((tokens[0]).trim().equals("host")) {
                    this.host = tokens[1].trim();
                } else if ((tokens[0]).trim().equals("port")) {
                    this.port = tokens[1].trim();
                } else if ((tokens[0]).trim().equals("database")) {
                    this.database = tokens[1].trim();
                } else if ((tokens[0]).trim().equals("user")) {
                    this.user = tokens[1].trim();
                } else if ((tokens[0]).trim().equals("password")) {
                    this.password = tokens[1].trim();
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataConnection.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fr.close();
            } catch (IOException ex) {
                Logger.getLogger(DataConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
//------------------------------------------------------------------------------
// Public Methods
//------------------------------------------------------------------------------
    
    public Statement getStatement() {
        Statement res = null;
        
        try {
            res = cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex) {
            Logger.getLogger(DataConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return res;
    }
            
    public void open() {
        try {
            cnn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port +"/" + database, user, password);                   
        } catch (SQLException ex) {
            Logger.getLogger(DataSet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void close() {
        try {
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void loadFromFile(String file) {
        loadConnectionData(file);
    }
    
    
}
